﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;

namespace HslCommunication.Profinet.OpenProtocol
{
	/// <summary>
	/// 开放以太网协议，仍然在开发中<br />
	/// Open Ethernet protocol, still under development
	/// </summary>
	// Token: 0x02000056 RID: 86
	public class OpenProtocolNet : NetworkDoubleBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x060006F4 RID: 1780 RVA: 0x00024933 File Offset: 0x00022B33
		public OpenProtocolNet()
		{
			base.ByteTransform = new RegularByteTransform();
		}

		/// <summary>
		/// 使用指定的IP地址及端口来初始化对象<br />
		/// Use the specified IP address and port to initialize the object
		/// </summary>
		/// <param name="ipAddress">Ip地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x060006F5 RID: 1781 RVA: 0x00024949 File Offset: 0x00022B49
		public OpenProtocolNet(string ipAddress, int port)
		{
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x060006F6 RID: 1782 RVA: 0x0002496F File Offset: 0x00022B6F
		protected override INetMessage GetNewNetMessage()
		{
			return new OpenProtocolMessage();
		}

		/// <inheritdoc />
		// Token: 0x060006F7 RID: 1783 RVA: 0x00024978 File Offset: 0x00022B78
		protected override OperateResult InitializationOnConnect(Socket socket)
		{
			OperateResult<string> operateResult = this.ReadCustomer(1, 0, 0, 0, null);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = operateResult.Content.Substring(4, 4) == "0002";
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					result = new OperateResult("Failed:" + operateResult.Content.Substring(4, 4));
				}
			}
			return result;
		}

		/// <summary>
		/// 自定义的命令读取
		/// </summary>
		/// <param name="mid"></param>
		/// <param name="revison"></param>
		/// <param name="stationId"></param>
		/// <param name="spindleId"></param>
		/// <param name="parameters"></param>
		/// <returns></returns>
		// Token: 0x060006F8 RID: 1784 RVA: 0x000249E8 File Offset: 0x00022BE8
		public OperateResult<string> ReadCustomer(int mid, int revison, int stationId, int spindleId, List<string> parameters)
		{
			bool flag = parameters != null;
			if (flag)
			{
				parameters = new List<string>();
			}
			OperateResult<byte[]> operateResult = OpenProtocolNet.BuildReadCommand(mid, revison, stationId, spindleId, parameters);
			bool flag2 = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag2)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag3 = !operateResult2.IsSuccess;
				if (flag3)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<string>(Encoding.ASCII.GetString(operateResult2.Content));
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060006F9 RID: 1785 RVA: 0x00024A6C File Offset: 0x00022C6C
		public override string ToString()
		{
			return string.Format("OpenProtocolNet[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 构建一个读取的初始报文
		/// </summary>
		/// <param name="mid"></param>
		/// <param name="revison"></param>
		/// <param name="stationId"></param>
		/// <param name="spindleId"></param>
		/// <param name="parameters"></param>
		/// <returns></returns>
		// Token: 0x060006FA RID: 1786 RVA: 0x00024A9C File Offset: 0x00022C9C
		public static OperateResult<byte[]> BuildReadCommand(int mid, int revison, int stationId, int spindleId, List<string> parameters)
		{
			bool flag = mid < 0 || mid > 9999;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>("Mid must be between 0 - 9999");
			}
			else
			{
				bool flag2 = revison < 0 || revison > 999;
				if (flag2)
				{
					result = new OperateResult<byte[]>("revison must be between 0 - 999");
				}
				else
				{
					bool flag3 = stationId < 0 || stationId > 9;
					if (flag3)
					{
						result = new OperateResult<byte[]>("stationId must be between 0 - 9");
					}
					else
					{
						bool flag4 = spindleId < 0 || spindleId > 99;
						if (flag4)
						{
							result = new OperateResult<byte[]>("spindleId must be between 0 - 99");
						}
						else
						{
							int count = 0;
							bool flag5 = parameters != null;
							if (flag5)
							{
								parameters.ForEach(delegate(string m)
								{
									count += m.Length;
								});
							}
							StringBuilder stringBuilder = new StringBuilder();
							stringBuilder.Append((20 + count).ToString("D4"));
							stringBuilder.Append(mid.ToString("D4"));
							stringBuilder.Append(revison.ToString("D3"));
							stringBuilder.Append('\0');
							stringBuilder.Append(stationId.ToString("D1"));
							stringBuilder.Append(spindleId.ToString("D2"));
							stringBuilder.Append('\0');
							stringBuilder.Append('\0');
							stringBuilder.Append('\0');
							stringBuilder.Append('\0');
							stringBuilder.Append('\0');
							bool flag6 = parameters != null;
							if (flag6)
							{
								for (int i = 0; i < parameters.Count; i++)
								{
									stringBuilder.Append(parameters[i]);
								}
							}
							stringBuilder.Append('\0');
							result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
						}
					}
				}
			}
			return result;
		}
	}
}
